
#include "utils.h"

#include <QtCore/QRect>
#include <QtGui/QImage>

/*----------------------------------------------------------------------------*/

typedef std::vector< QRect > QRectV;

typedef std::vector< QImage > QImageV;

/*----------------------------------------------------------------------------*/

class ImageSegmentator
{

/*----------------------------------------------------------------------------*/

public:

/*----------------------------------------------------------------------------*/

	ImageSegmentator(	QImage& _image
					,	int _imageSize
					,	int _imageContrast
					,	float _lineExpFactor
					,	float _lineTopFactor
					,	float _lineBotFactor
					,	float _wordLeftFactor
					,	float _wordRightFactor
					,	float _symbIntervalFactor
					,	float _symbBrightFactor
					,	float _symbWidthFactor
					);

	~ImageSegmentator();

/*----------------------------------------------------------------------------*/	

	QRectV& GetLinesFrames();

	QRectV& GetWordsFrames();

	QRectV& GetSymbolsFrames();

	QImageV& GetSymbolsImages();

	IntVV& GetSymbolsLayout();

/*----------------------------------------------------------------------------*/

private:

/*----------------------------------------------------------------------------*/

	QImage& m_image;

	QRectV m_linesFrames;

	QRectV m_wordsFrames;

	QRectV m_symbolsFrames;

	QImageV m_linesImages;

	QImageV m_wordsImages;

	QImageV m_symbolsImages;	

	IntVV m_symbolsLayout;

/*----------------------------------------------------------------------------*/

	/** general functions */

	float getPixelGreyBright( QImage& _image, int _i, int _j );

	FloatV getImageRowsBrightness( QImage& _image );

	FloatV getImageColsBrightness( QImage& _image );

	float getAverageBrightness( FloatV& _brightness );

	void setPixelGreyBright( QImage& _image, int _i, int _j, int _color );

	QImage contrastIncrease( QImage& _image, int _contrast );

	QImage imageDegradation( QImage& _image );

	void paintNearbyPixels( QImage& _image, int _i, int _j );

	/** lines */

	void findLinesFrames( float _lineExpFactor, float _lineTopFactor, float _lineBotFactor );

	bool isTopRow( int _currRow, FloatV& _rowsBrightness, float _topBright, float _botBright );

	bool isBotRow( int _currRow, FloatV& _rowsBrightness, float _topBright, float _botBright );

	void expandLinesBorders( float _lineExpFactor );

	void generateLinesImages();

	/** words */

	void findWordsFrames( int _contrast, float _wordLeftFactor, float _wordRightFactor );			

	bool isLeftCol( int _currPos, FloatV& _wordsBright, float _leftBright );

	bool isRightCol( int _currPos, FloatV& _wordsBright, float _rightBright );

	void generateWordsImages();

	/** symbols */

	void findSymbolsFrames( float _intervalFactor, float _brightFactor, float _widthFactor );

	IntV findLocalMinimums( FloatV& _wordColsBright, int _interval );

	int findMinimumBright( FloatV& _wordColsBright, int start, int end );

	IntV checkLocalMinimums( FloatV& _wordColsBright, IntV& _minimums, float _bright );

	bool isMinimum( int _currPos, FloatV& _symbolsBright, float _symBrightBorder );

	IntV removeCloseMinimums( IntV& _localMinimums, int _minHeight );
	
	void generateSymbolsImages( int _imageSize, int _contrast );

	/** resizing and scaling images */

	QImage prepareImage( QImage& _currImage, int _imageSize );	

	bool isStartBorder( int _currPos, FloatV& _brightness, float _bright );

	bool isEndBorder( int _currPos, FloatV& _brightness, float _bright );	

/*----------------------------------------------------------------------------*/

};

/*----------------------------------------------------------------------------*/